package com.itheima.bigevent.service.impl;

import com.itheima.bigevent.annotation.CategoryLog;
import com.itheima.bigevent.mapper.CategoryMapper;
import com.itheima.bigevent.pojo.Category;
import com.itheima.bigevent.pojo.User;
import com.itheima.bigevent.service.CategoryService;
import com.itheima.bigevent.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public int add(Category category) {
        // 获取当前用户
        User currentUser = ThreadLocalUtil.get();

        category.setCreateUser(currentUser.getId());
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());

        return categoryMapper.add(category);
    }

    @Override
    public List<Category> list() {
        // 获取当前登录用户
        User currentUser = ThreadLocalUtil.get();
        return categoryMapper.list(currentUser.getId());
    }

    @Override
    public Category detail(Integer id) {
        return categoryMapper.findById(id);
    }

    @CategoryLog
    @Override
    public void update(Category category) {
        category.setUpdateTime(LocalDateTime.now());
        categoryMapper.update(category);
    }

    @Override
    public int delete(Integer id) {
        return categoryMapper.delete(id);
    }
}
